import { _decorator, Component, director, find, instantiate, Node, Prefab } from 'cc';
import { EventEnum, MJStyleEnum } from '../script/Enum';
import GameController from '../script/GameController';
import { MJ } from './MJ';
const { ccclass, property } = _decorator;

@ccclass('Hand')
export class Hand extends Component {

    @property(Prefab)
    mjStand: Prefab = null;

    private list = [];


    protected onLoad(): void {
        director.on(EventEnum.EVENT_SHUFFLE, this.onShuffle, this);
        director.on(EventEnum.EVENT_INHAND, this.onInhand, this);
        director.on(EventEnum.EVENT_CHI, this.onChiEffect, this);
        director.on(EventEnum.EVENT_PENG, this.onPengEffect, this);
        director.on(EventEnum.EVENT_GANG, this.onGangEffect, this);

        this.onShuffle();
    }

    start() {

    }

    update(deltaTime: number) {
        // this.sortInHand();
    }

    private onShuffle() {
        console.log("更新授牌")
        let hands = GameController.getInstance().Hand;
        console.log(hands);
        for (let i = 0; i < hands.length; i++) {
            let mj = instantiate(this.mjStand);
            mj.getComponent(MJ).Style = MJStyleEnum.STYLE_STAND;
            mj.getComponent(MJ).MJValue = hands[i];


            this.node.addChild(mj);

            this.list.push(mj);
        }
        this.sortInHand();
    }

    public onInhand(mjValue) {
        console.log("新入手的是" + mjValue);

        let mj = instantiate(this.mjStand);
        mj.getComponent(MJ).Style = MJStyleEnum.STYLE_STAND;


        this.node.addChild(mj);


        this.list.push(mj);

        this.sortInHand();
    }

    public sortInHand() {

        for (let i = 0; i < this.list.length; i++) {
            let mj = this.list[i];
            console.log(GameController.getInstance().Hand[i]);
            mj.getComponent(MJ).MJValue = GameController.getInstance().Hand[i];
            mj.setPosition(-400 + (i % 8) * 100,
                -Math.floor(i / 8) * 120
            );
        }
    }

    private onChiEffect(mjValue) {

        console.log(mjValue);
        let add = 2;
        for (let i = this.list.length - 1; i >= 0; i--) {
            let mj = this.list[i];
            if (mj.getComponent(MJ).MJValue == mjValue + add) {
                mj.getComponent(MJ).onChiEffect();
                mj.parent = null;
                this.list.splice(i, 1);
                if (add == 0) {
                    break;
                }
                add--;
            }
        }
        this.sortInHand();
    }

    private onPengEffect(mjValue) {
        this.kickOutSameMJ(mjValue, 3);
        this.sortInHand();
    }

    private onGangEffect(mjValue) {
        this.kickOutSameMJ(mjValue, 4);
        this.sortInHand();
    }

    private onSevenPair() {

    }

    private onDragon() {

    }

    private onOneColor() {

    }

    private onThirteenOrphans() {

    }



    private kickOutSameMJ(mjValue, count) {
        for (let i = this.list.length - 1; i >= 0; i--) {
            let mj = this.list[i];
            if (mj.getComponent(MJ).MJValue == mjValue) {
                mj.getComponent(MJ).onChiEffect();
                mj.parent = null;
                this.list.splice(i, 1);
                if (count == 0) {
                    break;
                }
                count--;
            }
        }
    }
}

